package com.neu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.base.Result;
import com.neu.entity.FpAnalysisValue;
import com.neu.entity.FpFatherDemand;
import com.neu.entity.FpSecondDemand;
import com.neu.entity.Task;
import com.neu.mapper.FpAnalysisValueMapper;
import com.neu.service.FPAnalyVService;
import com.neu.service.FpFatherDemandService;
import com.neu.service.FpSecondDemandService;
import com.neu.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class FPAnalyVServiceImpl extends ServiceImpl<FpAnalysisValueMapper, FpAnalysisValue> implements FPAnalyVService {
    @Autowired
    private FpFatherDemandService fpFatherDemandService;

    @Autowired
    private FpSecondDemandService fpSecondDemandService;

    @Autowired
    TaskService taskService;

    /**
     * 计算总未调整功能点 totalufp
     * 计算内容：一个 fpaid 对应的所有子功能的 ufp
     * 一个任务对应多个父级功能，一个父级功能对应多个子级功能
     */
    public float calTotalUFP(String taskId) {
        // 获取当前任务的所有父级功能 (FpFatherDemand)
        List<FpFatherDemand> fatherDemands = fpFatherDemandService.lambdaQuery()
                .eq(FpFatherDemand::getTaskId, taskId) // 根据任务ID查询父级需求
                .list();

        // 初始化 totalufp 为 0
        float totalUfp = 0;

        // 遍历每个父级需求
        for (FpFatherDemand fatherDemand : fatherDemands) {
            // 获取当前父级功能下的所有子级功能 (FpSecondDemand)
            List<FpSecondDemand> secondDemands = fpSecondDemandService.lambdaQuery()
                    .eq(FpSecondDemand::getFid, fatherDemand.getId()) // 根据父级功能的ID查询子级需求
                    .list();

            // 累加子级需求的 ufp 值
            for (FpSecondDemand secondDemand : secondDemands) {
                // 调用子级需求的 calufp 方法来计算 ufp
                secondDemand.calufp();

                // 累加该子级功能的 ufp
                totalUfp += secondDemand.getUfp();
            }
        }

        // 返回计算出的 totalufp
        return totalUfp;
    }
    //添加cf
    @Override
    public boolean processCF(String taskId, float cf) {
        // 这里可以根据需求进行业务逻辑处理
        // 示例：打印收到的参数
        System.out.println("处理任务 ID：" + taskId);
        System.out.println("处理 CF 值：" + cf);

        // 模拟处理逻辑，返回处理结果
        return true; // 返回 true 表示处理成功，false 表示失败
    }
    @Override
    public FpAnalysisValue getByTaskId(String taskId) {
        // 使用 MyBatis-Plus QueryWrapper 查询

        return this.getOne(new QueryWrapper<FpAnalysisValue>().eq("task_id", taskId));
    }

    @Override
    public Result finallyCacul(Map map) {
        String taskId = (String) map.get("taskId");
        float totalufp = (Integer) map.get("totalufp");
        double vaf = (Double) map.get("vaf");
        Object obj = map.get("s");
        Integer S=0;
        if (obj instanceof Number) {
            S = ((Number) obj).intValue();
        }
       // double CF= (Double) map.get("cf");
        double DFP=totalufp*vaf;
        //转成float
        float dfp=(float)DFP;
        float s=(float) S;
       // float cf=(float) CF;

        //更新任务进度
        LambdaQueryWrapper<Task> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Task::getTaskId, taskId);
        Task task = taskService.getOne(queryWrapper1);
        task.setProgress(100);
        task.setTaskStatus("2");
        taskService.updateById(task);
        FpAnalysisValue analysisValue = new FpAnalysisValue();
        //判断是否已经存在
        LambdaQueryWrapper<FpAnalysisValue> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(FpAnalysisValue::getTaskId, taskId);
        FpAnalysisValue analysisValue1 = this.getOne(queryWrapper);
        if (analysisValue1 != null) {
            analysisValue1.setTotalufp(totalufp);
            analysisValue1.setDfp(dfp);
            analysisValue1.setS(s);
            return Result.success(this.updateById(analysisValue1));
        }
        Random random = new Random();
        int id = random.nextInt(100000);
        analysisValue.setFpaid(id);
        analysisValue.setTaskId(taskId);
        analysisValue.setTotalufp(totalufp);
        analysisValue.setDfp(dfp);
        analysisValue.setS(s);

        return Result.success(this.save(analysisValue));
    }


}
